package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.system.RoleNameDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.dao.system.UserDescDao;
import jsyf.oa.entity.system.CompanyEntity;
import jsyf.oa.entity.system.UserDescEntity;
import jsyf.oa.entity.system.UserEntity;
import jsyf.oa.facade.system.UserDescFacade;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/12/9 0009.
 */

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class UserDescService implements UserDescFacade {

    @Autowired
    private UserDescDao dao;
    @Autowired
    private UserDao userDao;

    @Override
    public String save(String jsonData) {
        UserDescEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), UserDescEntity.class);
        Integer rows = dao.add(entity);
        if (entity.getUser() != null) {
            UserEntity oldUser = (UserEntity) userDao.getIBaseEntityById(entity.getUser().getId());
            if (!StringUtils.isBlank(entity.getUser().getRoleId())) {
                oldUser.setRoleId(entity.getUser().getRoleId());
            }
            if (!StringUtils.isBlank(entity.getUser().getParentId())) {
                oldUser.setParentId(entity.getUser().getParentId());
            }
            userDao.update(oldUser);
        }
        return rows == 0 ? "" : entity.getId();
    }


    @Override
    public List<JSONObject> getUserDescByUserId(String userId) {
        String condition = " and USER_ID = ?";
        return dao.getJSONListByCondition(condition, new Object[]{userId});
    }
}
